<?php

class Perusahaan extends CActiveRecord
{
    /**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'digmigas.perusahaan';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('bentuk_badan_usaha,nama_perusahaan,jenis_usaha', 'required'),
			array('kota_id, telepon, fax, mobile_phone, kode_pos, created_by, modified_by', 'numerical', 'integerOnly'=>true),
			array('nama_perusahaan, pengurus, email, website', 'length', 'max'=>100),
			array('bentuk_badan_usaha', 'length', 'max'=>10),
			array('jenis_usaha,telepon', 'length', 'max'=>15),
			array('jenis_produk', 'length', 'max'=>50),
			array('alamat', 'length', 'max'=>200),
			array('kantor_cabang', 'length', 'max'=>5),
			array('created, modified', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, nama_perusahaan, pengurus, bentuk_badan_usaha, jenis_usaha, jenis_produk, alamat, kota_id, telepon, fax, mobile_phone, kode_pos, email, website, kantor_cabang, created, modified, created_by, modified_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                        'lainLains' => array(self::HAS_MANY, 'LainLain', 'perusahaan_id'),
                        'kilang' => array(self::HAS_MANY, 'Kilang', 'perusahaan_id'),
                        'izin' => array(self::HAS_ONE, 'Izin', 'perusahaan_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nama_perusahaan' => 'Nama Perusahaan',
			'pengurus' => 'Pengurus',
			'bentuk_badan_usaha' => 'Badan Usaha',
			'jenis_usaha' => 'Jenis Usaha',
			'jenis_produk' => 'Jenis Produk',
			'alamat' => 'Alamat',
			'kota_id' => 'Kota',
			'telepon' => 'Telepon',
			'fax' => 'Fax',
			'mobile_phone' => 'Mobile Phone',
			'kode_pos' => 'Kode Pos',
			'email' => 'Email',
			'website' => 'Website',
			'kantor_cabang' => 'Kantor Cabang',
			'upload_dokumen' => 'Upload Dokumen',
			'created' => 'Created',
			'modified' => 'Modified',
			'created_by' => 'Created By',
			'modified_by' => 'Modified By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

                $criteria->compare('t.id',$this->id,true);
		$criteria->compare('t.nama_perusahaan',$this->nama_perusahaan,true);
		$criteria->compare('t.pengurus',$this->pengurus,true);
		$criteria->compare('t.bentuk_badan_usaha',$this->bentuk_badan_usaha,true);
		$criteria->compare('t.jenis_usaha',$this->jenis_usaha,true);
                
                $criteria->with = array('kilang');
                $criteria->together = true;

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Perusahaan the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
        public function beforeSave()
        {
            if($this->isNewRecord)
            {
                $this->created = new CDbExpression('NOW()');
                $this->modified = new CDbExpression('NOW()');
                $this->created_by = Yii::app()->user->id;
                $this->modified_by = Yii::app()->user->id;
            } else {
                $this->modified = new CDbExpression('NOW()');
                $this->modified_by = Yii::app()->user->id;
            }
            return parent::beforeSave();
        }
}
